package com.dkd.applet.controller;

import cn.elegent.idem.annotation.ElegentIdem;
import cn.elegent.pay.ElegentPay;
import cn.elegent.pay.dto.PayRequest;
import cn.elegent.pay.dto.PayResponse;
import com.dkd.applet.domain.Order;
import com.dkd.applet.domain.dto.PayDto;
import com.dkd.applet.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
@Autowired
    OrderService orderService;
@Autowired
    ElegentPay elegentPay;
    //支付
    @ElegentIdem
    @PostMapping("/requestPay/{tradeType}/{platform}")
    public PayResponse requestPay(@RequestBody PayDto payDto,
    @PathVariable("tradeType")String tradeType, @PathVariable("platform")String platform){
//1.创建订单
        Order order = orderService.createOrder(payDto, platform);
//2.调用支付
        PayRequest param = new PayRequest();
        param.setBody(order.getSkuName());//商品名称
        param.setOrderSn(order.getOrderNo());//订单号
        param.setTotalFee(order.getAmount().intValue());//订单金额
        param.setOpenid(order.getOpenId());//openid
        return elegentPay.requestPay(param, tradeType, platform);

    }

}
